/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.unit.DataUnit;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.sun.mail.imap.protocol.BODY;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.jsoup.helper.DataUtil;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.parts.SparepartPricesSearchDTO;
import org.springblade.modules.admin.entity.MaintainPartsWork;
import org.springblade.modules.admin.entity.MaintainPlanContent;
import org.springblade.modules.admin.entity.SparepartPrices;
import org.springblade.modules.admin.enums.ApproveStatusEnum;
import org.springblade.modules.admin.enums.MaintainPlanContentEnum;
import org.springblade.modules.admin.enums.TaskStatusEnum;
import org.springblade.modules.admin.service.IMaintainPlanContentService;
import org.springblade.modules.admin.service.ISparepartPricesService;
import org.springblade.modules.admin.service.MaintainPartsWorkService;
import org.springblade.modules.admin.utils.SkuInfoUtil;
import org.springblade.modules.admin.vo.SparepartPricesVO;
import org.springblade.modules.admin.vo.parts.*;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * 控制器
 *
 * @author ysq
 * @since 2022-06-28
 */
@RestController
@AllArgsConstructor
@RequestMapping("admin/sparepartprices")
@Api(value = "报价审批", tags = "报价审批接口")
public class SparepartPricesController extends BladeController {

	private final ISparepartPricesService sparepartPricesService;
	private final MaintainPartsWorkService maintainPartsWorkService;
	private final IMaintainPlanContentService maintainPlanContentService;


	@PostMapping("update/partswork/extInfo")
	@ApiOperation(value = "更新配件的附件内容")
	public R<Boolean> updatePartswork(@RequestBody List<PartsWorkExtInfoVo> list) {
		String userName = AuthUtil.getUserName();
		Long userId = AuthUtil.getUserId();
		for (PartsWorkExtInfoVo partsWorkExtInfoVo : list) {
			if (partsWorkExtInfoVo.getCreateTime() == null) {
				partsWorkExtInfoVo.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
			}
			if (partsWorkExtInfoVo.getUserName() == null) {
				partsWorkExtInfoVo.setUserName(userName);
			}
			if (partsWorkExtInfoVo.getUserId() == null) {
				partsWorkExtInfoVo.setUserId(userId);
			}

		}
		Long workId = list.get(0).getPartsWorkId();
		MaintainPartsWork maintainPartsWork = new MaintainPartsWork();
		maintainPartsWork.setId(workId);
		maintainPartsWork.setExtInfo(list);
		return R.data(maintainPartsWorkService.updateById(maintainPartsWork));
	}


	@GetMapping("getPartIconList")
	@ApiOperation(value = "配件管理数量角标")
	public R<List<PartIconVo>> getPartIconList() {
		return R.data(sparepartPricesService.getPartIconList());
	}


	/**
	 * 获取配件的统计信息
	 *
	 * @return 数据集
	 */
	@GetMapping("/getPlanPartSummary")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "获取配件的统计信息")
	public R<PlanPartSummaryVo> getPlanPartSummary() {
		return R.data(sparepartPricesService.getPlanPartSummary());
	}


	/**
	 * 获取报价审批分页列表
	 *
	 * @param sparepartPrices
	 * @return 数据集
	 */
	@PostMapping("/planPage")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "配件分页", notes = "传入sparepartPrices")
	public R<IPage<SparepartPricesVO>> planPage(@RequestBody SparepartPricesSearchDTO sparepartPrices) {
		return R.data(sparepartPricesService.planPage(sparepartPrices));
	}


	/**
	 * 获取报价审批分页列表
	 *
	 * @param sparepartPrices
	 * @return 数据集
	 */
	@PostMapping("/page")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "报价审批分页", notes = "传入sparepartPrices")
	public R<IPage<SparepartPricesVO>> page(@RequestBody SparepartPricesSearchDTO sparepartPrices) {
		return R.data(sparepartPricesService.selectSparepartPricesPage(sparepartPrices));
	}


	/**
	 * 获取报价记录分页列表
	 *
	 * @param sparepartPrices
	 * @return 数据集
	 */
	@PostMapping("/list")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "报价记录分页", notes = "传入sparepartPrices")
	public R<IPage<SparepartPricesListVo>> list(@RequestBody SparepartPricesSearchDTO sparepartPrices) {
		return R.data(sparepartPricesService.list(sparepartPrices));
	}


	/**
	 * 获取报价记录分页列表
	 *
	 * @param sparepartPrices
	 * @return 数据集
	 */
	@PostMapping("/dispatchList")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "配件任务调度")
	public R<IPage<SparepartPricesVO>> dispatchList(@RequestBody SparepartPricesSearchDTO sparepartPrices) {
		return R.data(sparepartPricesService.dispatchList(sparepartPrices));
	}

	/**
	 * 2024-06-03 根据新记录新接口
	 * 获取报价记录分页列表 V2
	 * @param sparepartPrices 参数
	 * @return 数据集
	 */
	@PostMapping("/dispatchListV2")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "配件任务调度")
	public R<IPage<SparepartPricesVO>> dispatchListV2(@RequestBody SparepartPricesSearchDTO sparepartPrices) {
		return R.data(sparepartPricesService.dispatchListV2(sparepartPrices));
	}


	/**
	 * 获取报价明细
	 *
	 * @param id:配件记录的id值
	 * @return
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "审批报价详情明细")
	public R<SparepartPricesDetailVo> detail(@RequestParam(required = true) Long id) {

		return R.data(sparepartPricesService.detail(id));
	}


	/**
	 * 获取报价明细
	 *
	 * @return
	 */
	@PostMapping("/updateBatch")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "批量审批报价信息")
	public R<Boolean> update(@RequestBody List<SparepartPrices> sparepartPricesList) {

		return R.data(sparepartPricesService.updateBatch(sparepartPricesList));
	}

	@GetMapping("/getSparepartPricesDetail")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "报价记录明细")
	public R<SparepartPricesListDetailVo> getSparepartPricesDetail(@RequestParam(required = true) Long parentPlanId, @RequestParam(required = true) Long groupId) {

		return R.data(sparepartPricesService.getSparepartPricesDetail(parentPlanId, groupId));
	}


	/**
	 * 下载报价记录
	 *
	 * @param response
	 * @param sparepartPricesSearchDTO
	 * @throws IOException
	 */
	@PostMapping("/down/Detail/excel")
	public void downDetailExcel(HttpServletResponse response, @RequestBody SparepartPricesSearchDTO sparepartPricesSearchDTO) throws IOException {
		final List<SparepartPricesListVo> sparepartPricesVOS = sparepartPricesService.downDetailExcel(sparepartPricesSearchDTO);
		ExcelWriter excelWriter = null;
		ServletOutputStream outputStream = response.getOutputStream();
		try {
			List<List<String>> rows = new ArrayList<>();
			List<String> title = CollUtil.newArrayList("项目", "电梯","紧急程度","工单类型", "关联任务ID", "关联任务类型", "申请人",
				"报价员", "报价时间", "报价配件数量", "配件报价总额");
			rows.add(title);
			for (SparepartPricesListVo sparepartPricesVO : sparepartPricesVOS) {
				List<String> row = new ArrayList<>();
				row.add(sparepartPricesVO.getBuildingName());
				row.add(sparepartPricesVO.getElevatorAddress());
				row.add(sparepartPricesVO.getEmerProcTypeName());
				row.add(sparepartPricesVO.getWorkOrderTypeName());
				row.add(sparepartPricesVO.getParentPlanId().toString());
				row.add("维修");
				row.add(sparepartPricesVO.getCreateUserName());
				row.add(sparepartPricesVO.getPriceUserName());
				row.add(sparepartPricesVO.getPriceCheckTime());
				row.add(sparepartPricesVO.getPartNum());
				row.add(sparepartPricesVO.getAllPrice() == null ? "/" : sparepartPricesVO.getAllPrice());
				rows.add(row);
			}
			String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".xlsx";
			setResponseHeader(response, fileName);
			excelWriter = ExcelUtil.getBigWriter(rows.size());
			excelWriter.write(rows, true);
			excelWriter.flush(outputStream, true);
		} catch (
			Exception e) {
			e.printStackTrace();
		} finally {
			if (excelWriter != null) {
				excelWriter.close();
			}
			outputStream.close();
		}
	}


	/**
	 * 下载报价审批审批
	 *
	 * @param response
	 * @param sparepartPricesSearchDTO
	 * @throws IOException
	 */
	@PostMapping("/down/excel")
	public void downExcel(HttpServletResponse response, @RequestBody SparepartPricesSearchDTO sparepartPricesSearchDTO) throws IOException {
		final List<SparepartPricesVO> sparepartPricesVOS = sparepartPricesService.downExcel(sparepartPricesSearchDTO);
		ExcelWriter excelWriter = null;
		ServletOutputStream outputStream = response.getOutputStream();
		try {
			List<List<String>> rows = new ArrayList<>();
			List<String> title = CollUtil.newArrayList("项目", "电梯", "关联任务ID", "任务状态", "申请人", "配件名称",
				"配件型号", "数量", "申请时间", "转审时间", "报价员审批时间", "报价员", "报价状态", "配件报价总额");
			rows.add(title);
			for (SparepartPricesVO sparepartPricesVO : sparepartPricesVOS) {
				List<String> row = new ArrayList<>();
				row.add(sparepartPricesVO.getBuildingName());
				row.add(sparepartPricesVO.getElevatorAddress());

				row.add(Convert.toStr(sparepartPricesVO.getParentPlanId(), ""));
				if (StrUtil.isEmpty(Convert.toStr(sparepartPricesVO.getParentPlanId(), ""))) {
					row.add("");
				} else {
					row.add("维修");
				}
				row.add(sparepartPricesVO.getCreateUserName());
				final String[] split = sparepartPricesVO.getPartName().split(";");
				row.add(split[split.length - 1]);
				row.add(sparepartPricesVO.getPartNO());
				row.add(sparepartPricesVO.getPartNum());
				row.add(sparepartPricesVO.getCreateTime());
				row.add(sparepartPricesVO.getParentCheckTime());
				row.add(sparepartPricesVO.getPriceCheckTime());
				row.add(sparepartPricesVO.getPriceUserName());
				if (sparepartPricesVO.getPriceStatus() == 1) {
					row.add("已报价");
				} else if (sparepartPricesVO.getPriceStatus() == 0) {
					row.add("未报价");
				} else if (sparepartPricesVO.getPriceStatus() == 2) {
					row.add("待报价");
				}
				row.add(sparepartPricesVO.getAllPrice() == null ? "/" : sparepartPricesVO.getAllPrice());
				rows.add(row);
			}
			String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".xlsx";
			setResponseHeader(response, fileName);
			excelWriter = ExcelUtil.getBigWriter(rows.size());
			excelWriter.write(rows, true);
			excelWriter.flush(outputStream, true);
		} catch (
			Exception e) {
			e.printStackTrace();
		} finally {
			if (excelWriter != null) {
				excelWriter.close();
			}
			outputStream.close();
		}
	}


	/**
	 * 下载仓管审批
	 *
	 * @param response
	 * @param sparepartPricesSearchDTO
	 * @throws IOException
	 */
	@PostMapping("/down/warehouse/excel")
	public void downWarehouseExcel(HttpServletResponse response, @RequestBody SparepartPricesSearchDTO sparepartPricesSearchDTO) throws IOException {
		final List<SparepartPricesVO> sparepartPricesVOS = sparepartPricesService.downExcel(sparepartPricesSearchDTO);
		ExcelWriter excelWriter = null;
		ServletOutputStream outputStream = response.getOutputStream();
		try {
			List<List<String>> rows = new ArrayList<>();
			List<String> title = CollUtil.newArrayList("项目", "电梯", "关联任务ID", "关联任务类型", "申请人", "配件名称",
				"配件型号", "数量", "申请时间", "报价员审批时间", "仓库审批时间", "仓管员", "发货状态");
			rows.add(title);
			for (SparepartPricesVO sparepartPricesVO : sparepartPricesVOS) {
				List<String> row = new ArrayList<>();
				row.add(sparepartPricesVO.getBuildingName());
				row.add(sparepartPricesVO.getElevatorAddress());
				if (StrUtil.isEmpty(Convert.toStr(sparepartPricesVO.getParentPlanId(), ""))) {
					row.add("");
					row.add("");
				} else {
					row.add(Convert.toStr(sparepartPricesVO.getParentPlanId(), ""));
					row.add("维修");
				}
				row.add(sparepartPricesVO.getCreateUserName());
				String[] split = sparepartPricesVO.getPartName().split(";");
				row.add(split[split.length - 1]);
				row.add(sparepartPricesVO.getPartNO());
				row.add(sparepartPricesVO.getPartNum());
				row.add(sparepartPricesVO.getCreateTime());
				row.add(sparepartPricesVO.getPriceCheckTime());
				row.add(sparepartPricesVO.getWarehouseCreateTime());
				row.add(sparepartPricesVO.getWarehouseUserName());
				if (sparepartPricesVO.getWarehouseStatus() == 0) {
					row.add("未发货");
				} else if (sparepartPricesVO.getWarehouseStatus() == 1) {
					row.add("已发货");
				} else if (sparepartPricesVO.getWarehouseStatus() == 2) {
					row.add("待发货");
				}

				rows.add(row);
			}
			String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".xlsx";
			setResponseHeader(response, fileName);
			excelWriter = ExcelUtil.getBigWriter(rows.size());
			excelWriter.write(rows, true);
			excelWriter.flush(outputStream, true);
		} catch (
			Exception e) {
			e.printStackTrace();
		} finally {
			if (excelWriter != null) {
				excelWriter.close();
			}
			outputStream.close();
		}
	}


	/**
	 * 下载配件任务excel
	 *
	 * @param response
	 * @param sparepartPrices 搜索条件
	 * @throws IOException
	 */
	@PostMapping("/down/plan/excel")
	@ApiOperation("配件列表下载excel")
	public void downPlanExcel(HttpServletResponse response, @RequestBody SparepartPricesSearchDTO sparepartPrices) throws IOException {
		sparepartPrices.setSize(0);
		List<SparepartPricesVO> records = sparepartPricesService.planPage(sparepartPrices).getRecords();
		ExcelWriter excelWriter = null;
		ServletOutputStream outputStream = response.getOutputStream();
		try {
			List<List<String>> rows = new ArrayList<>();
			List<String> title = CollUtil.newArrayList("配件任务ID", "项目名称", "电梯梯号", "关联任务ID", "关联任务类型","关联任务计划完成时间", "申请人", "配件名称", "执行人", "实际执行人", "申请时间", "审核状态", "配件进度", "电梯状态");
			rows.add(title);
			for (SparepartPricesVO sparepartPricesVO : records) {
				List<String> row = new ArrayList<>();
				row.add(sparepartPricesVO.getPlanId().toString());
				row.add(sparepartPricesVO.getBuildingName());
				row.add(sparepartPricesVO.getElevatorAddress());
				row.add(Convert.toStr(sparepartPricesVO.getParentPlanId(), ""));
				// 注释：由于需求经battle后变更，这个字段用于存放来源类型名
				row.add(sparepartPricesVO.getPlanTypeFullName());

				if (!Func.isNull(sparepartPricesVO.getPlanEndTime())) {
					Date planEndTime = sparepartPricesVO.getPlanEndTime();
					row.add(DateUtil.format(planEndTime, "yyyy-MM-dd HH:mm:ss"));
				}else{
					row.add("");
				}
				row.add(sparepartPricesVO.getCreateUserName());
				if (StrUtil.isNotEmpty(sparepartPricesVO.getPartName())) {
					String[] split = sparepartPricesVO.getPartName().split("&amp;");
					List<String> partsName = new ArrayList<>();
					for (String s : split) {
						if(s.contains("&amp;")){
							String[] str = s.split("&amp;");
							partsName.add(str[str.length - 1]);
						}else if(s.contains(">")){
							String[] str = s.split(">");
							partsName.add(str[str.length - 1]);
						}else {
							partsName.add(s);
						}
					}
					row.add(String.join(",",partsName));
				} else {
					row.add("");
				}
				row.add(sparepartPricesVO.getUserNames());
				if (sparepartPricesVO.getSignUsers() != null && sparepartPricesVO.getSignUsers().size() > 0) {
					row.add(Convert.toStr(String.join(",", sparepartPricesVO.getSignUsers()), ""));
				} else {
					row.add("/");
				}
				row.add(sparepartPricesVO.getCreateTime());
				row.add(ApproveStatusEnum.getName(sparepartPricesVO.getApproveStatus()));
				row.add(Func.isNull(sparepartPricesVO.getStatusName()) ? "" : sparepartPricesVO.getStatusName());
				row.add(sparepartPricesVO.getIsStop() == 1 ? "正常" : "停梯");
				rows.add(row);
			}
			String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".xlsx";
			setResponseHeader(response, fileName);
			excelWriter = ExcelUtil.getBigWriter(rows.size());
			excelWriter.write(rows, true);
			excelWriter.flush(outputStream, true);
		} catch (
			Exception e) {
			e.printStackTrace();
		} finally {
			if (excelWriter != null) {
				excelWriter.close();
			}
			outputStream.close();
		}
	}


	/**
	 * 下载任务调用excel
	 *
	 * @param response
	 * @param sparepartPricesSearchDTO
	 * @throws IOException
	 */
	@PostMapping("/down/dispatch/excel")
	public void downDispatchExcel(HttpServletResponse response, @RequestBody SparepartPricesSearchDTO sparepartPricesSearchDTO) throws IOException {
		final List<SparepartPricesVO> sparepartPricesVOS = sparepartPricesService.downDispatchExcel(sparepartPricesSearchDTO);
		ExcelWriter excelWriter = null;
		ServletOutputStream outputStream = response.getOutputStream();
		try {
			List<List<String>> rows = new ArrayList<>();
			List<String> title = CollUtil.newArrayList("项目", "电梯", "配件工单ID", "关联任务ID","关联任务计划完成时间", "关联任务类型", "申请人", "配件名称",
				"配件型号", "数量", "申请时间", "分配时间", "任务状态", "执行人");
			rows.add(title);
			for (SparepartPricesVO sparepartPricesVO : sparepartPricesVOS) {
				List<String> row = new ArrayList<>();
				row.add(sparepartPricesVO.getBuildingName());
				row.add(sparepartPricesVO.getElevatorAddress());
				row.add(sparepartPricesVO.getPlanId().toString());
				row.add(Convert.toStr(sparepartPricesVO.getParentPlanId(), ""));
				// 时间转成 String 格式
				if (!Func.isNull(sparepartPricesVO.getPlanEndTime())) {
					Date planEndTime = sparepartPricesVO.getPlanEndTime();
					row.add(DateUtil.format(planEndTime, "yyyy-MM-dd HH:mm:ss"));
				}else{
					row.add("");
				}
				row.add(sparepartPricesVO.getPlanTypeFullName());
				row.add(sparepartPricesVO.getCreateUserName());
				if (StrUtil.isNotEmpty(sparepartPricesVO.getPartName())) {
					String[] split = sparepartPricesVO.getPartName().split("&amp;");
					List<String> partsName = new ArrayList<>();
					for (String s : split) {
						if(s.contains("&amp;")){
							String[] str = s.split("&amp;");
							partsName.add(str[str.length - 1]);
						}else if(s.contains(">")){
							String[] str = s.split(">");
							partsName.add(str[str.length - 1]);
						}else {
							partsName.add(s);
						}
					}
					row.add(String.join(",",partsName));
				} else {
					row.add("");
				}
				row.add(sparepartPricesVO.getPartNO());
				row.add(sparepartPricesVO.getPartNum());
				row.add(sparepartPricesVO.getCreateTime());
//				row.add(sparepartPricesVO.getTransferTime());
				row.add(sparepartPricesVO.getApportionTime());
				row.add(sparepartPricesVO.getStatusName());
				row.add(sparepartPricesVO.getUserNames());
				rows.add(row);
			}
			String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".xlsx";
			setResponseHeader(response, fileName);
			excelWriter = ExcelUtil.getBigWriter(rows.size());
			excelWriter.write(rows, true);
			excelWriter.flush(outputStream, true);
		} catch (
			Exception e) {
			e.printStackTrace();
		} finally {
			if (excelWriter != null) {
				excelWriter.close();
			}
			outputStream.close();
		}
	}


	/**
	 * 功能描述: 发送响应流方法
	 */
	private void setResponseHeader(HttpServletResponse response, String fileName) {
		try {
			try {
				fileName = new String(fileName.getBytes(), "ISO8859-1");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			response.setContentType("application/octet-stream;charset=ISO8859-1");
			response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
			response.addHeader("Pargam", "no-cache");
			response.addHeader("Cache-Control", "no-cache");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * 根据 parentId 父di
	 * @param parentId 父id
	 * @return 数据集
	 */
	@GetMapping("/get/relation/parts/plans")
	@ApiOperationSupport(order = 31)
	@ApiOperation(value = "配件分页", notes = "传入父id")
	public R<List<SparepartPricesVO>> getRelationPartsByParentId(Long parentId) {
		return R.data(sparepartPricesService.getRelationPartsByParentId(parentId));
	}

	/**
	 * 新抬头统计
	 */
	@GetMapping("/title/statistics/new")
	@ApiOperationSupport
	@ApiOperation(value = "抬头统计", notes = "传入查询条件")
	public R<Map<String,String>> titleStatistics() {
		// 当前租户下所有配件任务状态情况
		Map<String, String> resultMap = new HashMap<>(14);

		// 所有配件任务列表
		List<MaintainPlanContent> totalList = maintainPlanContentService.list(new LambdaQueryWrapper<MaintainPlanContent>()
			.eq(MaintainPlanContent::getTypeStatus, MaintainPlanContentEnum.MAINTAIN_PARTS.getId())
		);
		// 1. 任务总数
		resultMap.put("total", String.valueOf(totalList.size()));

		/**
		 * PARTS_APPROVE(1, MaintainPlanContentEnum.MAINTAIN_PARTS.getId(), "待审批"),
		 * 	PARTS_OFFER(2, MaintainPlanContentEnum.MAINTAIN_PARTS.getId(), "待报价"),
		 * 	PARTS_REPLY(10, MaintainPlanContentEnum.MAINTAIN_PARTS.getId(), "待回签"),
		 * 	PARTS_PROCUREMENT(11, MaintainPlanContentEnum.MAINTAIN_PARTS.getId(), "待采购"),
		 * 	PARTS_SHIPMENTS(3, MaintainPlanContentEnum.MAINTAIN_PARTS.getId(), "待发货"),
		 * 	PARTS_RECEIVE(12, MaintainPlanContentEnum.MAINTAIN_PARTS.getId(), "待收货"),
		 * 	PARTS_ALLOCATION(4, MaintainPlanContentEnum.MAINTAIN_PARTS.getId(), "待分配"),
		 * 	PARTS_TO_START(5, MaintainPlanContentEnum.MAINTAIN_PARTS.getId(), "待更换"),
		 * 	PARTS_CHANGE(6, MaintainPlanContentEnum.MAINTAIN_PARTS.getId(), "更换中"),
		 * 	PARTS_FINISH(7, MaintainPlanContentEnum.MAINTAIN_PARTS.getId(), "已完成"),
		 * 	PARTS_CLOSE(8, MaintainPlanContentEnum.MAINTAIN_PARTS.getId(), "已关闭"),
		 * 	PARTS_WITHDRAW(9, MaintainPlanContentEnum.MAINTAIN_PARTS.getId(), "已撤回")
		 */
		// 2. 待审批
		long approveCount = totalList
			.stream().filter(f -> f.getStatus().equals(TaskStatusEnum.PARTS_APPROVE.getStatus())).count();
		resultMap.put("waitApprove", String.valueOf(approveCount));
		// 3. 待报价

		long offerCount = totalList
			.stream().filter(f -> f.getStatus().equals(TaskStatusEnum.PARTS_OFFER.getStatus())).count();
		resultMap.put("waitOffer", String.valueOf(offerCount));
		// 4. 待发货

		long shipmentsCount = totalList
			.stream().filter(f -> f.getStatus().equals(TaskStatusEnum.PARTS_SHIPMENTS.getStatus())).count();
		resultMap.put("waitShipments", String.valueOf(shipmentsCount));
		// 5. 待收货

		long receiveCount = totalList
			.stream().filter(f -> f.getStatus().equals(TaskStatusEnum.PARTS_RECEIVE.getStatus())).count();
		resultMap.put("waitReceive", String.valueOf(receiveCount));
		// 6. 待分配

		long allocationCount = totalList
			.stream().filter(f -> f.getStatus().equals(TaskStatusEnum.PARTS_ALLOCATION.getStatus())).count();
		resultMap.put("waitAllocation", String.valueOf(allocationCount));
		// 7. 待更换

		long toStartCount = totalList
			.stream().filter(f -> f.getStatus().equals(TaskStatusEnum.PARTS_TO_START.getStatus())).count();
		resultMap.put("waitToStart", String.valueOf(toStartCount));
		// 8. 更换中

		long changeCount = totalList
			.stream().filter(f -> f.getStatus().equals(TaskStatusEnum.PARTS_CHANGE.getStatus())).count();
		resultMap.put("change", String.valueOf(changeCount));
		// 9. 已完成

		long finishCount = totalList
			.stream().filter(f -> f.getStatus().equals(TaskStatusEnum.PARTS_FINISH.getStatus())).count();
		resultMap.put("finish", String.valueOf(finishCount));
		// 10. 已关闭

		long closeCount = totalList
			.stream().filter(f -> f.getStatus().equals(TaskStatusEnum.PARTS_CLOSE.getStatus())).count();
		resultMap.put("close", String.valueOf(closeCount));
		// 11. 已撤回

		long withdrawCount = totalList
			.stream().filter(f -> f.getStatus().equals(TaskStatusEnum.PARTS_WITHDRAW.getStatus())).count();
		resultMap.put("withdraw", String.valueOf(withdrawCount));
		// 12. 待回签

		long replyCount = totalList
			.stream().filter(f -> f.getStatus().equals(TaskStatusEnum.PARTS_REPLY.getStatus())).count();
		resultMap.put("waitReply", String.valueOf(replyCount));
		// 13. 待采购

		long procurementCount = totalList
			.stream().filter(f -> f.getStatus().equals(TaskStatusEnum.PARTS_PROCUREMENT.getStatus())).count();
		resultMap.put("waitProcurement", String.valueOf(procurementCount));

		// 14. 已退回，单独查询
		int rejectCount = maintainPartsWorkService.rejectCount();
		resultMap.put("reject", String.valueOf(rejectCount));

		return R.data(resultMap);
	}

}
